package ca.jsbr.as2js_lib.utils.assets 
{
	import ca.jsbr.as2js_lib.event.BasicEvent;
	import ca.jsbr.as2js_lib.event.EventDispatcher;
	import ca.jsbr.as2js_lib.event.RequestEvent;
	import ca.jsbr.as2js_lib.utils.DataLoader;
	import ca.jsbr.as2js_lib.utils.generic.PathUtils;
	import flash.events.Event;
	import flash.utils.Dictionary;
	import flash.xml.XMLDocument;
	import js.element.HTMLElement;
	import js.element.HTMLImageElement;
	import js.Node;
	/**
	 * ...
	 * @author jsbr [me@jsbr.ca]
	 */
	public class AssetsFactory extends EventDispatcher 
	{
		static private var _instance:AssetsFactory;
		static public const IMAGE:String = "image";
		static public const AUDIO:String = "audio";
		static public const VIDEO:String = "video";
		
		private var _load:DataLoader;
		private var _assets:Dictionary;
		private var _wait:Array;
		
		public function AssetsFactory() 
		{
			_assets = new Dictionary();
		}
		
		
		public function load(path:String):void
		{
			//trace("AssetsFactory", "load");
			unload();
			_load = new DataLoader();
			_load.addEventListener(RequestEvent.COMPLETE, onMainLoadComplet);
			_load.load(path);
		}
		
		private function onMainLoadComplet(e:RequestEvent):void 
		{
			readDataXML(_load.getXmlNode());
		}
		
		public function readDataXML(xml:XMLDocument):void 
		{
			for (var i:int = 0; i < xml.childNodes.length;i++ )
			{
				var node:Node = xml.childNodes[i];
				if (node.nodeType == 1) {
					var name:String = node.attributes.getNamedItem("id").nodeValue;
					createAsset(name,node.firstChild.nodeValue);					
				}
			}
		}
		
		private function createAsset(name:String,value:String):void
		{
			trace(value);
			if (PathUtils.getFileType(value)==PathUtils.TYPE_IMAGE)
			{
				var img:HTMLImageElement = document.createElement("img");
				_wait++;
				img.addEventListener("load", onLoadComplete);
				img.src = value;
				_assets[name] = img;
			}
			else if (PathUtils.getFileType(value) == PathUtils.TYPE_JSON)
			{
				var subLoader:DataLoader = new DataLoader();
				subLoader.addEventListener(RequestEvent.COMPLETE, onJsonLoadComplete);
				subLoader.load(value);
				subLoader.name = name;
				_wait++;
				_assets[name] = subLoader;
			}
		}
		
		private function onJsonLoadComplete(e:Event):void 
		{
			var obj:Object = e.currentTarget.getJSON();
			if (obj.image) {
				if (obj.relative)
					createAsset("__$temp__", PathUtils.getBasePath(e.currentTarget.path) + "/" + obj.image);
				else
					createAsset("__$temp__", obj.image);
				obj.image = _assets["__$temp__"];
				_assets["__$temp__"] = null;
			}
			_assets[e.currentTarget.name] = obj;
			onLoadComplete(null);
		}
		
		private function onLoadComplete(e:Event):void 
		{
			_wait--;
			if (_wait<=0)
			{
				dispatchEvent(new BasicEvent(BasicEvent.READY));
			}
		}
		
		public function unloadAsset(name:String):void
		{
			
		}
		
		public function unload():void
		{
			_assets = new Dictionary();
		}
		
		public function addAsset(name:String,path:String):void
		{
			
		}
		
		public function getAssets(id:String):Object 
		{
			return _assets[id]
		}
		
		public function destroy():void
		{
			_instance = null;
		}
		
		static public function getInstance():AssetsFactory 
		{
			if (!_instance)
				_instance = new AssetsFactory();
			return _instance;
		}
		
		
		
	}

}